/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook.util;

import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author zach
 */
public class FilterHash extends CustomHash {

    public static final String SAVE_DELIM = ",";
    
    public FilterHash() {
        super();
    }
    
    public FilterHash(String loadStr) {
        super();
        load(loadStr);
    }
    
    public boolean filter(String key) {
        if (containsKey(key)) {
            return ((Boolean)get(key)).booleanValue();
        }
        return true;
    }
    
    public boolean filter(String key, String val) {
        if (containsKey(key,val)) {
            return ((Boolean)get(key,val)).booleanValue();
        }
        return true;
    }
    
    public void load(String str) {
        String[] values = str.split(SAVE_DELIM);
        for (int i = 0; i < values.length; i++) {
            put(values[i],new Boolean(false));
        }
    }
    
    public String save() {
        Iterator keys = keySet().iterator();
        Vector vals = new Vector();
        while(keys.hasNext()) {
            String key = (String)keys.next();
            if (!filter(key)) {
                vals.add(vals);
            }
        }
        return DatabookLib.arrayJoin(vals.toArray(new String[0]), SAVE_DELIM);
    }
}
